aes(), it becomes an axis and must be mapped to a variable nameaes() (and in the geometry), it’s a setting applied to the entire geometry# We've seen this before
mtcars <- mtcars %>%
mutate(Transmission = factor(am, labels = c('Automatic','Manual')))
ggplot(mtcars, aes(x = mpg, y = hp, color = Transmission)) +
geom_point()ggplot(mtcars, aes(x = mpg, y = hp, color = Transmission,
size = wt, shape = Transmission)) +
geom_point()ggplot(mtcars, aes(x = Transmission, fill = factor(cyl))) +
geom_bar(position = 'dodge', linetype = 'dashed', color = 'black')coord_flip it for bar graphs)theme().help(theme)library(ggalt)
mtcars2 <- mtcars %>% group_by(Transmission) %>% summarize(count = n())
ggplot(mtcars2, aes(x = Transmission,y=count)) + geom_lollipop()# geom_lollipop in particular has a 'horizontal' option anyway
# so this is just for demonstration
ggplot(mtcars2, aes(x = Transmission,y=count)) +
geom_lollipop(color = 'red', size = 2) + coord_flip()axis.text and the ticks we mark on the scaleelement_text provides text objectsggplot(mtcars2, aes(x = Transmission,y=count)) +
geom_lollipop(color = 'red', size = 2) + coord_flip() +
labs(x = '', y = '') +
scale_y_continuous(breaks = c(10,20), limits = c(0,20)) +
theme(axis.text.x = element_text(size = 12,family='serif'),
axis.text.y = element_text(size = 16,family='serif', angle = 10))limitsggplot(mtcars2, aes(x = Transmission,y=count)) +
geom_lollipop(color = 'red', size = 2) + coord_flip() +
scale_x_discrete(limits = c("Manual", "Automatic"))axis.line and element_lineaxis.line or even line) or specifically (axis.ticks.x)panel to change what goes behind that geometry! element_rect might come up!element_blank()element_text (and elsewhere, like annotation or geom_text), hjust and vjust align text horizontally and vertically. Set 0/1 for L/R or Top/Bottomlegend.position is in portion of the frame. .8 = 80% to the right.ggplot(mtcars, aes(x = mpg, y = hp, color = Transmission)) + geom_point() +
theme(legend.text = element_text(hjust = .5, family = 'serif'),
legend.title = element_text(hjust = 1, family = 'serif', face = 'bold'),
legend.background = element_rect(color = 'black', fill = 'white'),
legend.position = c(.8,.7))theme() settings as an object to use repeatedly as a house styletheme() customization afterwards.theme_ settings you can tack on. I only use a few…library(ggthemes)
ggplot(mtcars, aes(x = mpg, y = hp, color = Transmission)) + geom_point() +
theme_tufte()# devtools::install_github('bbc/bbplot')
library(bbplot)
ggplot(mtcars, aes(x = mpg, y = hp, color = Transmission)) + geom_point() +
bbc_style()library(gghighlight); data(gapminder, package = 'gapminder')
ggplot(gapminder, aes(x = year, y = lifeExp, color=country)) + geom_line(size = 1.5) +
labs(x = NULL, y = "Life Expectancy", title = "North America Only") +
scale_x_continuous(limits=c(1950,2015),
breaks = c(1950,1970,1990,2010))+
gghighlight(country %in% c('United States','Canada','Mexico'),
unhighlighted_params = aes(size=.1),
label_params=list(direction='y',nudge_x=10)) +
theme_minimal(base_family='serif') geom_dl from directlabelslibrary(directlabels)
ggplot(gapminder %>% filter(country %in% c('United States','Canada','Mexico')),
aes(x = year, y = lifeExp, color=country)) + geom_line(size = 1.5) +
scale_x_continuous(limits = c(1950,2025))+ theme_minimal()+guides(color=FALSE)+
geom_dl(aes(x = year + 2,label = country),method = 'last.bumpup')annotate() function, but this takes some workremotes::install_github ("mattcowgill/ggannotate"), not install.packages()p <- ggplot(gapminder %>% filter(country %in% c('United States','Canada','Mexico')),
aes(x = year, y = lifeExp, color=country)) + geom_line(size = 1.5) +
scale_x_continuous(limits = c(1950,2025))+ theme_minimal()+guides(color=FALSE)+
geom_dl(aes(x = year + 2,label = country),method = 'last.bumpup')
ggannotate::ggannotate(p)ggsave() will save our result to file (or we can Export Image in the Plots frame of RStudio)+ to stick together, | to put “beside”, - to move to next column, / for the next rowggplot(mtcars, aes(x = mpg, y = hp)) + geom_point()